package com.geovis.boot.service.demo.file.security;

import com.alibaba.fastjson.JSONObject;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

//认证失败
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException auth) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
//        PrintWriter print = response.getWriter();
        //登录失败计入失败次数。
        //更新user表数据，更新次数 + 1 ，失败时间
//        String userName = request.getParameter("username").toString();
        String userName = request.getParameter("username").toString();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg", "登录失败,请检查用户密码信息！");
        jsonObject.put("code", 202);
        String jsonStr = JSONObject.toJSONString(jsonObject);
        PrintWriter print = response.getWriter();
        print.print(jsonStr);
        print.flush();
        print.close();
    }

}